//
//  oldSubServicesViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/3/13.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldSubServicesViewController.h"
#import "oldBasedNormalTableViewCell.h"
#import "oldService.h"
#import "oldServiceDetailInfoViewController.h"

@interface oldSubServicesViewController ()
@property (nonatomic, copy)NSArray *dataArray;
@end

static NSString * const identifier = @"oldBasedNormalTableViewCell";

@implementation oldSubServicesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = _cname;
    [self rigisterCell:identifier];
}

#pragma mark - initData
- (void)initData
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: _pid, @"pid", nil];
    
    WeakSelf
    
    [oldHub waiting:@"加载中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_GetServeCategory params:params success:^(id data) {
        [oldHub hideHUD];
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < [data count]; i++) {
        oldService *service = [[oldService alloc] initWithDictionary:data[i]];
        [array addObject:service];
    }
    
    _dataArray = array;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldBasedNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldBasedNormalTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    oldService *service = _dataArray[indexPath.row];
    
    cell.label_title.text = service.cname;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldServiceDetailInfoViewController *vc = [oldServiceDetailInfoViewController new];
    vc.service = _dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
